import { _decorator, Component, Node, Vec3, Camera } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 相机控制器
 * 
 * 功能：
 * 1. 跟随目标对象（如玩家）
 * 2. 平滑相机移动
 * 3. 维持固定视角和距离
 * 4. 支持动态目标切换
 * 
 * 特点：
 * 1. 可配置的跟随参数（高度、距离等）
 * 2. 平滑的相机过渡
 * 3. 自动朝向目标
 * 4. 支持相机碰撞检测
 */
@ccclass('CameraController')
export class CameraController extends Component {
    @property(Node)
    private target: Node | null = null;

    @property
    private distance: number = 10;

    @property
    private height: number = 15;

    @property
    private smoothSpeed: number = 0.125;

    @property(Camera)
    private camera: Camera | null = null;

    private targetPosition: Vec3 = new Vec3();
    private currentPosition: Vec3 = new Vec3();

    update(deltaTime: number) {
        if (!this.target || !this.camera) return;

        // 计算目标位置
        this.targetPosition.set(
            this.target.position.x,
            this.target.position.y + this.height,
            this.target.position.z - this.distance
        );

        // 平滑移动相机
        Vec3.lerp(
            this.currentPosition,
            this.camera.node.position,
            this.targetPosition,
            this.smoothSpeed
        );

        this.camera.node.position = this.currentPosition;
        this.camera.node.lookAt(this.target.position);
    }

    public setTarget(target: Node) {
        this.target = target;
    }
} 